<!DOCTYPE HTML>
<html lang="zh-CN">




<head>
    <meta charset="utf-8">
    <meta name="keywords" content="&#39;JavaNote05_Lambda&#39;, 深度学习 故障诊断 Java">
    <meta name="description" content="河海大学 | 计算机与信息学院 | 信号与信息处理">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>&#39;JavaNote05_Lambda&#39; | 世庆的博客</title>
    <link rel="icon" type="image/png" href="/shiqing/favicon.png">

    <link rel="stylesheet" type="text/css" href="/shiqing/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/shiqing/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/shiqing/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/shiqing/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/shiqing/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/shiqing/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/shiqing/css/my.css">

    <script src="/shiqing/libs/jquery/jquery.min.js"></script>

    <script src="https://sdk.jinrishici.com/v2/browser/jinrishici.js" charset="utf-8"></script>
<meta name="generator" content="Hexo 6.3.0"></head>




    <body>
        <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/shiqing/" class="waves-effect waves-light">
                    
                    <img src="/shiqing/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">世庆的博客</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/shiqing/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/shiqing/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">世庆的博客</div>
        <div class="logo-desc">
            
            河海大学 | 计算机与信息学院 | 信号与信息处理
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/shiqing/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/sqtian-hhu/" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/sqtian-hhu/" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

        



<div class="bg-cover pd-header post-cover" style="background-image: url('/shiqing/medias/featureimages/15.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">&#39;JavaNote05_Lambda&#39;</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/shiqing/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/shiqing/tags/Java%E5%9F%BA%E7%A1%80/">
                                <span class="chip bg-color">Java基础</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/shiqing/categories/Java/" class="post-category">
                                Java
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-03-19
                </div>
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/shiqing/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="Lambda表达式"><a href="#Lambda表达式" class="headerlink" title="Lambda表达式"></a>Lambda表达式</h2><p>3.1 函数式编程思想概述<br>尽量忽略面向对象的复杂语法，强调做什么，而不是以什么形式做</p>
<p>面向对象的思想：做一件事情，找一个能解决这个事情的对象，调用对象的方法，完成事情<br>函数式编程思想：只要能获取到结果，怎么做的都不重要，重视的是结果，不重视过程</p>
<span id="more"></span>

<h2 id="3-2-冗余的Runnable代码传统写法public-class-Demo01Runnable-public-static-void-main-String-args-RunnableImpl-run-x3D-new-RunnableImpl-Thread-t-x3D-new-Thread-run-t-start-for-int-i-x3D-0-i-lt-20-i-System-out-println-Thread-currentThread-getName-”–-gt-”-i"><a href="#3-2-冗余的Runnable代码传统写法public-class-Demo01Runnable-public-static-void-main-String-args-RunnableImpl-run-x3D-new-RunnableImpl-Thread-t-x3D-new-Thread-run-t-start-for-int-i-x3D-0-i-lt-20-i-System-out-println-Thread-currentThread-getName-”–-gt-”-i" class="headerlink" title="3.2 冗余的Runnable代码传统写法public class Demo01Runnable {    public static void main(String[] args) {        RunnableImpl run &#x3D; new RunnableImpl();        Thread t &#x3D; new Thread(run);        t.start();        for (int i &#x3D; 0; i &lt; 20; i++) {            System.out.println(Thread.currentThread().getName()+”–&gt;”+i);        }    }}"></a>3.2 冗余的Runnable代码<br>传统写法<br>public class Demo01Runnable {<br>    public static void main(String[] args) {<br>        RunnableImpl run &#x3D; new RunnableImpl();<br>        Thread t &#x3D; new Thread(run);<br>        t.start();<br>        for (int i &#x3D; 0; i &lt; 20; i++) {<br>            System.out.println(Thread.currentThread().getName()+”–&gt;”+i);<br>        }<br>    }<br>}</h2><p>匿名内部类简化：<br>public class Demo01Runnable {<br>    public static void main(String[] args) {<br>        Runnable task &#x3D; new Runnable() {<br>            @Override<br>            public void run() {<br>                System.out.println(“多任务执行”);<br>            }<br>        };<br>        new Thread(task).start();<br>    }<br>}</p>
<hr>
<p>为了实现任务不得不创建了匿名内部类，但真正希望做的事情是：将run方法体内的代码传递给Thread类知晓<br>传递一段代码才是真正的目的<br>public class Demo02Lambda {<br>    public static void main(String[] args) {<br>        &#x2F;&#x2F;使用匿名内部类的方式实现多线程<br>        new Thread(new Runnable() {<br>            @Override<br>            public void run() {<br>                System.out.println(Thread.currentThread().getName()+”多任务执行”);<br>            }<br>        }).start();</p>
<pre><code>    //使用Lambda表达式实现多线程
    new Thread(()-&gt; &#123;
            System.out.println(Thread.currentThread().getName()+&quot;多任务执行&quot;);
        &#125;
    ).start();
&#125;
</code></pre>
<h2 id=""><a href="#" class="headerlink" title="}"></a>}</h2><p>匿名内部类的好处和弊端<br>一方面可以帮助我们省去实现类的定义，另一方面，语法太复杂</p>
<p>Lambda语法中<br>()-&gt; System.out.println(Thread.currentThread().getName()+”多任务执行”);<br>()即run方法的参数,接口中抽象方法的参数列表<br>-&gt; 表示将参数传递给后面的代码<br>后面的输出语句即业务逻辑代码,重写抽象方法的方法体</p>
<p>标准格式: (参数类型 参数名称) -&gt; {代码语句}</p>
<h2 id="有参数有返回"><a href="#有参数有返回" class="headerlink" title="有参数有返回"></a>有参数有返回</h2><p>&#x2F;*<br>    Lambda表达式有参数有返回值的练习<br>    需求:<br>        使用数组存储多个Person对象<br>        对数组中多个Person对象使用Arrays方法通过年龄进行升序排序<br> *&#x2F;<br>public class Demo01Arrays {<br>    public static void main(String[] args) {<br>        &#x2F;&#x2F;使用数组存储多个Person对象<br>        Person[] arr &#x3D; {new Person(“CC”,18),<br>            new Person(“TT”,15),<br>            new Person(“AB”,8)};<br>        Arrays.sort(arr, new Comparator<Person>() {<br>            @Override<br>            public int compare(Person o1, Person o2) {<br>                return o1.getAge()-o2.getAge();<br>            }<br>        });</p>
<pre><code>    //遍历数组
    for (Person p : arr) &#123;
        System.out.println(p);
    &#125;
    // 使用Lambda表达式简化匿名内部类,换成降序
    Arrays.sort(arr,(Person o1, Person o2) -&gt;&#123;
        return o2.getAge()-o1.getAge();
    &#125;);
    for (Person p : arr) &#123;
        System.out.println(p);
    &#125;
&#125;
</code></pre>
<p>}</p>
<hr>
<p>给定一个计算器Calculator接口,内含抽象方法calc可以将两个int数字相加得到和值<br>public interface Calculator {<br>    int calc(int a,int b);<br>}</p>
<p>使用Lambda标准格式调用invokeCalc方法,完成120,130的相加计算<br>public class Demo01Calculator {<br>    public static void main(String[] args) {<br>        &#x2F;&#x2F;匿名内部类<br>        invokeCalc(10, 20, new Calculator() {<br>            @Override<br>            public int calc(int a, int b) {<br>                return a+b;<br>            }<br>        });</p>
<pre><code>    //Lambda表达式
    invokeCalc(120, 130,(int a, int b)-&gt;&#123;return a+b;&#125;);
&#125;

public static void invokeCalc(int a,int b,Calculator c) &#123;
    int sum = c.calc(a,b);
    System.out.println(sum);
&#125;
</code></pre>
<p>}</p>
<h2 id="Lambda省略格式"><a href="#Lambda省略格式" class="headerlink" title="Lambda省略格式"></a>Lambda省略格式</h2><p>可推导可省略<br>可以省略的内容:<br>    1. (参数列表):括号中的参数列表的数据类型,可以省略不写<br>    2. (参数列表:括号中的参数如果只有一个,那么类型和()都可以省略<br>    3. {一些代码}:如果{}中的代码只有一行,无论是否有返回值,都可以省略({},return,分号)<br>        注意:要省略,{},return,分号就得一起省略</p>
<p>Lambda方法的使用前提</p>
<ol>
<li>使用Lambda必须具有接口,且要求接口中有且仅有一个抽象方法</li>
<li>使用Lambda必须具有上下文推断,也就是方法的参数或局部变量类型必须为Lambda对应的接口类型,<br>才能使用Lambda作为该接口的实例<br>有且仅有一个抽象方法的接口称为函数式接口</li>
</ol>
<h2 id="函数式接口"><a href="#函数式接口" class="headerlink" title="函数式接口"></a>函数式接口</h2><p>函数式接口在Java中指有且只有一个抽象方法的接口<br>&#x2F;*<br>    函数式接口只有一个抽象方法，<br>    当然接口中可以包含其他的方法(默认,静态,私有)<br>    @FunctionalInterface注解,检测接口是否是一个函数式接口<br> *&#x2F;<br>@FunctionalInterface<br>public interface MyFunctionalInterface {<br>    &#x2F;&#x2F;定义一个抽象方法<br>    public abstract void method();<br>}</p>
<p>使用：<br>&#x2F;*<br>    函数式接口的使用:一般作为方法的参数和返回值类型<br> *&#x2F;<br>public class Demo {<br>    &#x2F;&#x2F;定义一个方法,参数使用函数式接口<br>    public static void show(MyFunctionalInterface myinter) {<br>        myinter.method();<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //调用show方法,方法参数是一个接口,所以可以传递接口的实现类对象MyFunctionalInterfaceImpl
    //或者使用匿名内部类
    show(new MyFunctionalInterface() &#123;
        @Override
        public void method() &#123;
            System.out.println(&quot;使用匿名内部类重写接口中的抽象方法&quot;);
        &#125;
    &#125;);
    //或者lambda表达式
    show(() -&gt; System.out.println(&quot;使用Lambda表达式重写接口中的抽象方法&quot;));
&#125;
</code></pre>
<p>}</p>
<h2 id="函数式编程"><a href="#函数式编程" class="headerlink" title="函数式编程"></a>函数式编程</h2><p>有些场景的代码执行后，结果不一定会被使用，从而造成性能的浪费，Lambda表达式是延迟执行的，正好可以作为解决方案</p>
<p>性能浪费的日志案例<br>注：日志可以帮助我们快速定位问题，记录程序运行中的情况，以便项目的监控和优化<br>&#x2F;*<br>    使用Lambda优化日志案例<br>    Lambda特点:延迟加载<br>    Lambda使用前提:存在函数式接口<br> *&#x2F;<br>public class Demo02Lambda {<br>    &#x2F;&#x2F;定义一个显示日志方法,参数传递等级和MessageBuilder接口<br>    public static void showLog(int level,MessageBuilder mb) {<br>        &#x2F;&#x2F;对日志的等级进行判断<br>        if(level &#x3D;&#x3D; 1){<br>            System.out.println(mb.builderMessage());<br>        }<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //定义三个日志信息
    String msg1 = &quot;Hello&quot;;
    String msg2 = &quot;world&quot;;
    String msg3 = &quot;Java&quot;;
    
    //函数式接口使用Lambda
    showLog(1,()-&gt;&#123;
        return msg1+msg2+msg3;
    &#125;);
    
    /*
        使用Lambda表达式作为参数传递,仅仅是把参数传递到showLog方法中
        只有满足条件,日志的等级是1级,才会调用接口
        才会进行字符串的拼接
        如果条件不满足
        MessageBuilder接口中的方法builderMessage就不会执行
        
     */
&#125;
</code></pre>
<h2 id="-1"><a href="#-1" class="headerlink" title="}"></a>}</h2><p>Lambda作为参数和返回值<br>public class Demo01Runnable {<br>    &#x2F;&#x2F;定义一个方法startThread(Runnable run)<br>    public static void startThread(Runnable run) {<br>        new Thread(run).start();<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    startThread(()-&gt; System.out.println(Thread.currentThread().getName()+&quot;--&gt;&quot;+&quot;线程启动了&quot;));
&#125;
</code></pre>
<p>}</p>
<p>返回函数式接口<br>&#x2F;*<br>    如果一个方法的返回值类型是一个函数式接口,那么就可以直接返回一个Lambda表达式<br>    当需要通过一个方法获取一个java.util.Comparator接口类型的对象作为排序器时,就可以调该方法获取</p>
<p> *&#x2F;<br>public class Demo02Comparator {<br>    &#x2F;&#x2F;定义一个方法,方法的返回值类型使用函数式接口Comparator<br>    public static Comparator<String> getComparator(){<br>        &#x2F;&#x2F;方法返回值是一个函数式接口,使用Lambda<br>&#x2F;&#x2F;		return (String o1, String o2)-&gt; {<br>&#x2F;&#x2F;				&#x2F;&#x2F;降序<br>&#x2F;&#x2F;				return o2.length()-o1.length();<br>&#x2F;&#x2F;		};<br>        &#x2F;&#x2F;进一步优化,String可以省略,大括号省略,return省略<br>        return (o1, o2)-&gt; o2.length()-o1.length();<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //创建一个字符串组
    String[] arr = &#123;&quot;aaa&quot;,&quot;b&quot;,&quot;ccccc&quot;,&quot;dddddddddddddd&quot;&#125;;
    //输出排序前的数组
    System.out.println(Arrays.toString(arr));//[aaa, b, ccccc, dddddddddddddd]
    //调用Arrays中的sort方法排序
    Arrays.sort(arr,getComparator());
    System.out.println(Arrays.toString(arr));//[dddddddddddddd, ccccc, aaa, b]
&#125;
</code></pre>
<p>}</p>
<h2 id="常用的函数式接口"><a href="#常用的函数式接口" class="headerlink" title="常用的函数式接口"></a>常用的函数式接口</h2><p>java.util.function</p>
<ol>
<li>Supplier接口<br>&#x2F;*<br> Supplier<T>接口仅包含一个无参方法:T get() 用来获取一个泛型参数指定类型的对象数据<br> Supplier<T>接口被称为生产型接口,指定接口的泛型是什么类型,那么接口中的get方法就会产生声明类型的数据</li>
</ol>
<p> *&#x2F;<br>public class Demo01Supplier {<br>    &#x2F;&#x2F;定义一个方法,方法的参数传递Supplier<T>接口,泛型执行String,get方法就会返回一个String<br>    public static String getString(Supplier<String> sup){<br>        return sup.get();<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //调用getString方法,方法的参数Supplier是一个函数时接口
    String s = getString(()-&gt;&#123;
        //生产一个字符串,并返回
        return &quot;TSQ&quot;;
    &#125;);
    System.out.println(s);
&#125;
</code></pre>
<p>}</p>
<p>练习：<br>使用Supplier接口作为方法参数类型，通过Lambda表达式求出int数组中的最大值<br>提示：接口泛型使用Integer<br>public class Demo02Test {<br>    &#x2F;&#x2F;定义一个方法,用于获取int数据类型数组中元素的最大值,方法参数传递Supplier接口,泛型使用Integer<br>    public static int getMax(Supplier<Integer> sup){<br>        return sup.get();<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //定义一个int类型的数组,并赋值
    int[] arr = &#123;100,0,-50,88,99,33,-30&#125;;
    //调用getMax方法,方法的参数Supplier是一个函数式接口,可传递Lambda表达式
    int maxValue = getMax(()-&gt;&#123;
        //获取数组的最大值,并返回
        //定义一个变量,把数组中第一个元素赋值给该变量,记录数组中元素的最大值
        int max = arr[0];
        for (int i : arr) &#123;
            if(i&gt;max)&#123;
                max = i;
            &#125;
        &#125;
        return max;
    &#125;);
    System.out.println(maxValue);
&#125;
</code></pre>
<p>}</p>
<hr>
<ol start="2">
<li>Consumer接口<br>Cunsumer接口是一个消费型接口，泛型执行什么类型，就可以使用accept方法消费什么类型的数据</li>
</ol>
<p>public class Demo01Consumer {<br>    &#x2F;*<br>        定义一个方法<br>        方法的参数传递一个字符串的姓名<br>        方法的参数传递Consumer接口,泛型使用String<br>        可以使用Consumer接口消费字符串的姓名</p>
<pre><code> */
public static void method(String name, Consumer&lt;String&gt; con) &#123;
    con.accept(name);
&#125;

public static void main(String[] args) &#123;
    //调用method方法,传递字符串姓名,方法的另一个参数时Consumer接口
    method(&quot;西门吹雪&quot;,(String name)-&gt;&#123;
        //对传递的字符串进行消费
        //消费方式:直接输出字符串
        System.out.println(name);
        //消费方式:把字符串进行反转输出
        String reName = new StringBuilder(name).reverse().toString();
        System.out.println(reName);
    &#125;);
&#125;
</code></pre>
<p>}</p>
<hr>
<p>默认方法：andThen<br>&#x2F;*<br>    Consumer接口的默认方法andThen<br>    作用:需要两个Consumer接口,可以把两个Consumer接口组合到一起,再对数据进行消费<br>    例如:<br>    Consumer<String> con1<br>    Consumer<String> con2<br>    String s &#x3D; “hello”;<br>    con1.accept(s);<br>    con2.accept(s);<br>    &#x2F;&#x2F;等同于<br>    con1.andThen(con2).accept(s);&#x2F;&#x2F;谁写前面谁先消费</p>
<p> *&#x2F;<br>public class Demo02AndThen {<br>    &#x2F;&#x2F;定义一个方法,方法传递一个字符串和两个Consumer接口<br>    public static void method(String s, Consumer<String> con1,Consumer<String> con2){<br>        con1.accept(s);<br>        con2.accept(s);<br>        &#x2F;&#x2F;使用andThen方法把两个接口连接到一起<br>        con1.andThen(con2).accept(s);<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    method(&quot;Hello&quot;,(t)-&gt;&#123;
        //消费方式,把字符串转换为大写输出
        System.out.println(t.toUpperCase());
    &#125;,(t)-&gt;&#123;
        System.out.println(t.toLowerCase());
    &#125;);
&#125;
</code></pre>
<p>}</p>
<p>练习：<br>格式化打印信息<br>public class Demo03Test {<br>    public static void printInfo(String[] arr, Consumer<String> con1,Consumer<String> con2){<br>        &#x2F;&#x2F;遍历字符串数组<br>        for (String message : arr) {<br>            con1.andThen(con2).accept(message);<br>        }<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //定义一个字符串类型的数组
    String[] arr = &#123;&quot;西门吹雪,男&quot;,&quot;绯村剑心,男&quot;,&quot;小龙女,女&quot;&#125;;

    //调用printInfo方法,传递一个字符串数组,和两个Lambda表达式
    printInfo(arr,(message)-&gt;&#123;
        //消费方式:对message切割,获取姓名,按照指定格式输出
        String name = message.split(&quot;,&quot;)[0];
        System.out.print(&quot;姓名:&quot;+name);
    &#125;,(message)-&gt;&#123;
        String age = message.split(&quot;,&quot;)[1];
        System.out.println(&quot;。性别:&quot;+age+&quot;。&quot;);
    &#125;);
&#125;
</code></pre>
<p>}</p>
<ol start="3">
<li><p>Predicate接口<br>对某种类型的数据进行判断<br>public class Demo01Predicate {<br> &#x2F;*<br> 定义一个方法<br> 参数传递一个String类型的字符串<br> 传递一个Predicate接口，泛型使用String<br> 使用Predicate中的方法test对字符串进行判断，并把判断的结果返回<br>  *&#x2F;<br> public static boolean checkString(String s, Predicate<String> pre) {<br> return pre.test(s);<br> }</p>
<p> public static void main(String[] args) {<br> &#x2F;&#x2F;定义一个字符串<br> String s &#x3D; “abcde”;<br> &#x2F;&#x2F;调用checkString方法对字符串进行校验<br> boolean b &#x3D; checkString(s,(String str)-&gt;{<br>     &#x2F;&#x2F;对参数传递的字符串进行判断,判断字符串的长度是否大于5<br>     return str.length()&gt;5;<br> });<br> System.out.println(b);<br> }</p>
</li>
</ol>
<h2 id="-2"><a href="#-2" class="headerlink" title="}"></a>}</h2><p>默认方法：and<br>&#x2F;*<br>    逻辑表达式:可以连接多个判断条件<br>    &amp;&amp;:与运算符,有false则false<br>    &#x2F;&#x2F;:或运算符,有true则true<br>    !:非,取反</p>
<pre><code>需求:判断一个字符串,有两个判断条件
    1. 判断字符串的长度是否大于5
    2. 判断字符串是否包含a
    两个条件同时满足,我们可以用&amp;&amp;连接两个条件
    Predicate接口中有一个方法and,也可以用于连接两个判断条件
</code></pre>
<p> <em>&#x2F;<br>public class Demo02Predicate_and {<br>    &#x2F;</em><br>        定义一个方法,传递一个字符串,两个Predicate接口<br>        一个判断条件一,一个判断条件二</p>
<pre><code> */

public static boolean checkString(String s, Predicate&lt;String&gt; pre1,Predicate&lt;String&gt; pre2) &#123;
    //return pre1.test(s) &amp;&amp; pre2.test(s);
    //等价于
    return pre1.and(pre2).test(s);
&#125;

public static void main(String[] args) &#123;
    //定义一个字符串
    String s = &quot;abcdef&quot;;
    boolean b = checkString(s,(String str)-&gt;&#123;
        return str.length()&gt;5;
    &#125;,(String str)-&gt;&#123;
        return str.contains(&quot;a&quot;);
    &#125;);
    System.out.println(b);
&#125;
</code></pre>
<h2 id="-3"><a href="#-3" class="headerlink" title="}"></a>}</h2><p>默认方法：or<br>或<br>&#x2F;&#x2F;return pre1.test(s) || pre2.test(s);<br>&#x2F;&#x2F;等价于<br>return pre1.or(pre2).test(s);<br>默认方法：negate<br>取反<br>&#x2F;&#x2F;return !pre.test(s);<br>return pre.negate().test(s);</p>
<p>练习:<br>集合信息筛选<br>&#x2F;*<br>    练习:集合信息筛选<br>    数组当中有多条”姓名+性别”的信息如下,<br>    String[] arr &#x3D; {“西门吹雪,男”,”绯村剑心,男”,”小龙女,女”,”杨过,男”};<br>    通过Predicate接口的拼装将符合条件的字符串筛选到ArrayList中<br>    同时满足<br>    1. 男生<br>    2. 姓名4个字<br> *&#x2F;<br>public class Demo05Test {<br>    public static ArrayList<String> filter(String[] arr, Predicate<String> pre1,Predicate<String> pre2){<br>        ArrayList<String> list &#x3D; new ArrayList&lt;&gt;();<br>        for (String s : arr) {<br>            boolean b &#x3D; pre1.and(pre2).test(s);<br>            if(b){<br>                &#x2F;&#x2F;条件满足,信息存入list<br>                list.add(s);<br>            }<br>        }<br>        return list;<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    String[] arr = &#123;&quot;西门吹雪,男&quot;,&quot;绯村剑心,男&quot;,&quot;小龙女,女&quot;,&quot;杨过,男&quot;&#125;;
    ArrayList&lt;String&gt; list = filter(arr,(String s)-&gt;&#123;
        return s.split(&quot;,&quot;)[0].length()==4;
    &#125;,(String s)-&gt;&#123;
        return s.split(&quot;,&quot;)[1].equals(&quot;男&quot;);
    &#125;);

    //遍历集合
    for (String s : list) &#123;
        System.out.println(s);
    &#125;
&#125;
</code></pre>
<p>}</p>
<ol start="4">
<li><p>Function接口<br>用来根据一个类型的数据得到另一个类型的数据<br>抽象方法:R apply(T t), 根据类型T的参数获取类型R的结果<br>public class Demo01Function {<br> &#x2F;*<br> 定义一个方法<br> 方法参数传递一个字符串类型的整数<br> 方法的参数传递一个Function接口,泛型使用&lt;String,Integer&gt;<br> 使用Function接口中的方法apply,把字符串类型的整数转换为Integer类型的整数<br>  *&#x2F;</p>
<p> public static void change(String s, Function&lt;String,Integer&gt; fun){<br> Integer in &#x3D; fun.apply(s);<br> System.out.println(in);<br> }</p>
<p> public static void main(String[] args) {<br> &#x2F;&#x2F;定义一个字符串类型的整数<br> String s &#x3D; “1234”;<br> &#x2F;&#x2F;调用change方法<br> change(s,(String str)-&gt;{<br>     &#x2F;&#x2F;把字符串类型的整数转换为Integer类型的整数返回<br>     return Integer.parseInt(str);<br> });<br> }</p>
</li>
</ol>
<h2 id="-4"><a href="#-4" class="headerlink" title="}"></a>}</h2><p>默认方法:andThen<br>import java.util.function.Function;</p>
<p>&#x2F;*<br>    Function接口中的默认方法andThen:用来进行组合操作<br>    需求:<br>        把String类型的”123”,转换为Integer类型,把转换后的结果加10<br>        把增加后的Integer类型数据转换为String类型<br>    分析:<br>        转换流两次<br>        第一次是把String类型转换为Integer类型<br>            所以我们可以使用Function&lt;String,Integer&gt; fun1<br>            Integer i &#x3D; fun1.apply(“123”)+10;<br>        第二次把Integer转换为String类型<br>            String s &#x3D; fun2.apply(i);</p>
<pre><code>    我们可以使用andThen方法,把两次转换组合到一起使用
    String s = fun1.andThen(fun2).apply(&quot;123&quot;);
</code></pre>
<p> *&#x2F;<br>public class Demo02Function_andThen {<br>    public static void change(String s, Function&lt;String,Integer&gt; fun1, Function&lt;Integer,String&gt; fun2){<br>        String ss &#x3D; fun1.andThen(fun2).apply(s);<br>        System.out.println(ss);<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    //定义一个字符串方法
    String s = &quot;123&quot;;
    //调用change方法
    change(s,(String str)-&gt;&#123;
        //把字符串转换为整数+10
        return Integer.parseInt(str)+10;
    &#125;,(Integer in)-&gt;&#123;
        //把整数转换为字符串
        return in+&quot;&quot;;
    &#125;);
&#125;
</code></pre>
<h2 id="-5"><a href="#-5" class="headerlink" title="}"></a>}</h2><p>练习:<br>自定义函数模型拼接<br>&#x2F;*<br>    练习:自定义函数模型拼接<br>    请使用Function进行函数模型的拼接,按照顺序需要执行的多个函数操作为<br>        String str &#x3D; “西门吹雪,20”;<br>    分析:<br>    1. 将字符串截取数字年龄部分,得到字符串;<br>        Function&lt;String,String&gt; “西门吹雪,20”-&gt;”20”<br>    2. 将上一步字符串转换为int类型<br>    Function&lt;String,Integer&gt; “20”-&gt;20<br>    3. 将上一步的int数字累加100,得到int数字<br>    Function&lt;Integer,Integer&gt; 20-&gt;120</p>
<p> *&#x2F;<br>public class Demo06Test {<br>    public static int change(String s, Function&lt;String,String&gt; fun1,<br>                             Function&lt;String,Integer&gt; fun2,Function&lt;Integer,Integer&gt; fun3){<br>        &#x2F;&#x2F;使用andThen<br>        return fun1.andThen(fun2).andThen(fun3).apply(s);<br>    }</p>
<pre><code>public static void main(String[] args) &#123;
    String str = &quot;西门吹雪,20&quot;;
    //调用change方法
    int num = change(str,(String s)-&gt;&#123;
        return s.split(&quot;,&quot;)[1];
    &#125;,(String s)-&gt;&#123;
        return Integer.parseInt(s);
    &#125;,(Integer i)-&gt;&#123;
        return i+100;
    &#125;);
    System.out.println(num);
&#125;
</code></pre>
<p>}</p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/shiqing/about" rel="external nofollow noreferrer">ShiQing</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://shiqing.gitee.io/shiqing/2020/03/19/java-ji-chu/javanote05-lambda/">https://shiqing.gitee.io/shiqing/2020/03/19/java-ji-chu/javanote05-lambda/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/shiqing/about" target="_blank">ShiQing</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/shiqing/tags/Java%E5%9F%BA%E7%A1%80/">
                                    <span class="chip bg-color">Java基础</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/shiqing/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/shiqing/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/shiqing/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/shiqing/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/shiqing/2020/03/26/java-ji-chu/javanote06-iostream/">
                    <div class="card-image">
                        
                        
                        <img src="/shiqing/medias/featureimages/21.jpg" class="responsive-img" alt="&#39;JavaNote06_IOStream&#39;">
                        
                        <span class="card-title">&#39;JavaNote06_IOStream&#39;</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            IO概述I：inputO：output流：数据，一个字符&#x3D;2个字节&#x3D;16个二进制位java.io字节流：一切文件数据存储时都是以二进制数字的形式保存的
字节输出流:java.io.OutputStream,此抽象类是表示
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-03-26
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/shiqing/categories/Java/" class="post-category">
                                    Java
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/shiqing/tags/Java%E5%9F%BA%E7%A1%80/">
                        <span class="chip bg-color">Java基础</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/shiqing/2020/03/18/java-ji-chu/javanote04-thread/">
                    <div class="card-image">
                        
                        
                        <img src="/shiqing/medias/featureimages/3.jpg" class="responsive-img" alt="&#39;JavaNote04_Thread&#39;">
                        
                        <span class="card-title">&#39;JavaNote04_Thread&#39;</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            多线程
并发与并行并发：指两个或多个事件在同一时间段内发生   交替执行,一个人吃两个馒头并行：指两个或多个事件在同一时刻发生(同时发生)   同时执行,两个人一人一个个馒头

                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-03-18
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/shiqing/categories/Java/" class="post-category">
                                    Java
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/shiqing/tags/Java%E5%9F%BA%E7%A1%80/">
                        <span class="chip bg-color">Java基础</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/shiqing/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/shiqing/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/shiqing/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/shiqing/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/shiqing/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




        <footer class="page-footer bg-color">
    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2024</span>
            
            <span id="year">2019</span>
            <a href="/shiqing/about" target="_blank">ShiQing</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/blinkfox" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2528393970@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>



    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2528393970" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2528393970" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







    <a href="/shiqing/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


        <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/shiqing/search.xml', 'searchInput', 'searchResult');
});
</script>

        <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


        <script src="/shiqing/libs/materialize/materialize.min.js"></script>
        <script src="/shiqing/libs/masonry/masonry.pkgd.min.js"></script>
        <script src="/shiqing/libs/aos/aos.js"></script>
        <script src="/shiqing/libs/scrollprogress/scrollProgress.min.js"></script>
        <script src="/shiqing/libs/lightGallery/js/lightgallery-all.min.js"></script>
        <script src="/shiqing/js/matery.js"></script>

        <!-- Baidu Analytics -->

        <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

        
        <script src="/shiqing/libs/others/clicklove.js" async="async"></script>
        
        
        <script async src="/shiqing/libs/others/busuanzi.pure.mini.js"></script>
        

        <script type="text/javascript">
            var OriginTitile = document.title,
                st;
            document.addEventListener("visibilitychange", function () {
                document.hidden ? (document.title = "看不见我~看不见我~", clearTimeout(st)) : (document.title =
                    "(๑•̀ㅂ•́) ✧被发现了～", st = setTimeout(function () {
                        document.title = OriginTitile
                    }, 3e3))
            })
        </script>

        <!-- 背景樱花飘落特效 -->
        
            <script type="text/javascript">
                //只在桌面版网页启用特效
                var windowWidth = $(window).width();
                if (windowWidth > 768) {
                    document.write('<script type="text/javascript" src="/js/xuehuapiaoluo.js"><\/script>');
                }
            </script>
        

        

        


        
        

        

        

        
        <script src="/shiqing/libs/instantpage/instantpage.js" type="module"></script>
        

    </body>

</html>
